package com.fenshi.interactor.imp;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.fenshi.bean.RequestBean;
import com.fenshi.interactor.SendOrderInteractor;
import com.fenshi.presenter.listener.RequestListener;
import com.fenshi.until.MethodCode;
import com.fenshi.until.MethodType;
import com.quickdv.helper.NetWorkImp;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lilongfei on 15/9/21.
 */
public class SendOrderInteractorImp extends NetWorkImp implements SendOrderInteractor {
    RequestListener<JSONObject> listener;
    public SendOrderInteractorImp(Context c, RequestListener<JSONObject> listener) {
        super(c);
        this.listener = listener;
    }

    @Override
    protected void onRequestSuccess(Object tag, JSONObject result) {

        if (result == null){
            onRequestExcption(null);
            return;
        }
        JSONObject response = result.getJSONObject(MethodCode.responseContent_key);
        if (response == null) {
            onRequestExcption(null);
        } else {
            int  statue = response.getInteger(MethodCode.responseStatue);

            if (statue == MethodCode.NetState.SUCCESS.getValue()){

                    JSONObject obj = response.getJSONObject(MethodCode.OBJECT);
                    listener.Success(tag + "", obj);

            }else //TODO 未完成
                    listener.Error(tag + "", response.getString("messageInfo"));
        }
    }

    @Override
    protected void onRequestExcption(VolleyError error) {
        listener.Exception("",error);
    }

    /**
     *
     * @param userId
     * @param cardId
     * @param carId
     * @param carType
     * @param orderType
     * @param sendType 租用类型
     * @param currentTime
     * @param startTime
     * @param endTime
     */
    @Override
    public void sendOrder(String userId, String cardId, String carId, String carType, int orderType, int sendType, String currentTime, String startTime, String endTime) {
        RequestBean bean = new RequestBean();
        Map<String,Object> params = new HashMap<>();
        params.put("carId",carId);
//        params.put("tenancyType",sendType);
//        params.put("sessionId",userId);
        params.put("cardId",cardId);
//        params.put("startTime",startTime);
//        params.put("endTime",endTime);
        bean.setRequestParams(params);
        bean.setUserSession(userId);
        bean.setRequestCode(MethodType.MANAGER_RENT);
        addFastJsonQueue(Request.Method.POST, MethodType.MANAGER_RENT, (JSONObject) JSON.toJSON(bean));
        startRequest();
    }
}
